const { query } = require("../db/query")

module.exports = {
  // 获取全部宠物信息列表
  getAllPetList: async () => {
    // 调用query发送sql语句到mysql进行获取全部宠物信息列表
    return await query("select * from petinformation");
  },
  // 获取宠物信息列表
  getPetList: async (current, pageSize) => {
    if (current && pageSize) {
      const currentSize = (current - 1) * pageSize;
      let sql = `
      SELECT 
      petinformation.id,
      petinformation.user_id,
      petcategory.pet_type,
      petinformation.pet_variety,
      petinformation.pet_gender,
      petinformation.pet_age,
      petinformation.pet_vaccin,
      petinformation.pet_sterilization,
      petinformation.pet_Insect_repellent,
      petinformation.pet_images,
      petinformation.pet_giveaddress,
      petinformation.pet_intro,
      petinformation.pet_release_status
      FROM
      petinformation,petcategory 
      WHERE
      petinformation.category_id = petcategory.id
      LIMIT ?,?;
      `
      // 调用query发送sql语句到mysql进行获取宠物信息列表
      return await query(sql, [currentSize, parseInt(pageSize)])
    } else {
      let sql = `
      SELECT 
      petinformation.id,
      petinformation.user_id,
      petcategory.pet_type,
      petinformation.pet_variety,
      petinformation.pet_gender,
      petinformation.pet_age,
      petinformation.pet_vaccin,
      petinformation.pet_sterilization,
      petinformation.pet_Insect_repellent,
      petinformation.pet_images,
      petinformation.pet_giveaddress,
      petinformation.pet_intro,
      petinformation.pet_release_status
      FROM
      petinformation,petcategory 
      WHERE
      petinformation.category_id = petcategory.id
      `
      // 调用query发送sql语句到mysql进行获取宠物信息列表
      return await query(sql)
    }

  },
  // 通过审核状态是否为空来获取宠物信息列表
  getPetListOfIsNull: async () => {
    let sql = `
    SELECT 
    petinformation.id,
    petinformation.user_id,
    petcategory.pet_type,
    petinformation.pet_variety,
    petinformation.pet_gender,
    petinformation.pet_age,
    petinformation.pet_vaccin,
    petinformation.pet_sterilization,
    petinformation.pet_Insect_repellent,
    petinformation.pet_images,
    petinformation.pet_giveaddress,
    petinformation.pet_intro,
    petinformation.pet_release_status
    FROM
    petinformation,petcategory 
    WHERE
    petinformation.category_id = petcategory.id
    AND 
    petinformation.pet_release_status IS NULL;
    `
    // 调用query发送sql语句到mysql进行获取宠物信息列表
    return await query(sql)
  },
  // 通过审核状态来获取宠物信息列表
  getPetListOfStatus: async (pet_release_status) => {
    let sql = `
    SELECT 
    petinformation.id,
    petinformation.user_id,
    petcategory.pet_type,
    petinformation.pet_variety,
    petinformation.pet_gender,
    petinformation.pet_age,
    petinformation.pet_vaccin,
    petinformation.pet_sterilization,
    petinformation.pet_Insect_repellent,
    petinformation.pet_images,
    petinformation.pet_giveaddress,
    petinformation.pet_intro,
    petinformation.pet_release_status
    FROM
    petinformation,petcategory 
    WHERE
    petinformation.category_id = petcategory.id
    AND 
    petinformation.pet_release_status =?;
    `
    // 调用query发送sql语句到mysql进行获取宠物信息列表
    return await query(sql, [pet_release_status])
  },
  // 通过宠物发布审核
  adoptRelease: async (id) => {
    // 调用query发送sql语句到mysql进行通过宠物发布审核
    return await query("update petinformation set pet_release_status = 'true' where id =?", [id])
  },
  // 否决宠物发布审核
  vetoRelease: async (id) => {
    // 调用query发送sql语句到mysql进行否决宠物发布审核
    return await query("update petinformation set pet_release_status = 'false' where id =?", [id])
  },
  getPetListLimitNull: async (current, pageSize) => {
    const currentSize = (current - 1) * pageSize;
    let sql = `
    SELECT 
    petinformation.id,
    petinformation.user_id,
    petcategory.pet_type,
    petinformation.pet_variety,
    petinformation.pet_gender,
    petinformation.pet_age,
    petinformation.pet_vaccin,
    petinformation.pet_sterilization,
    petinformation.pet_Insect_repellent,
    petinformation.pet_images,
    petinformation.pet_giveaddress,
    petinformation.pet_intro,
    petinformation.pet_release_status
    FROM
    petinformation,petcategory 
    WHERE
    petinformation.category_id = petcategory.id
    AND 
    petinformation.pet_release_status IS NULL
    LIMIT ?,?;
    `
    // 调用query发送sql语句到mysql进行获取宠物信息列表
    return await query(sql, [currentSize, parseInt(pageSize)])
  },
  getPetListLimitStatus: async (current, pageSize) => {
    const currentSize = (current - 1) * pageSize;
    let sql = `
    SELECT 
    petinformation.id,
    petinformation.user_id,
    petcategory.pet_type,
    petinformation.pet_variety,
    petinformation.pet_gender,
    petinformation.pet_age,
    petinformation.pet_vaccin,
    petinformation.pet_sterilization,
    petinformation.pet_Insect_repellent,
    petinformation.pet_images,
    petinformation.pet_giveaddress,
    petinformation.pet_intro,
    petinformation.pet_release_status
    FROM
    petinformation,petcategory 
    WHERE
    petinformation.category_id = petcategory.id
    AND 
    petinformation.pet_release_status =?
    LIMIT ?,?;
    `
    // 调用query发送sql语句到mysql进行获取宠物信息列表
    return await query(sql, [currentSize, parseInt(pageSize)])
  },
}